home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 264_01 / touch.c < prev    next >
Text File  |  1980-01-01  |  2KB  |  77 lines

  1. /*
  2.  * touch - set time of files to current time
  3.  *
  4.  * Usage: touch [-c] file...
  5.  * Exit status is # of files that couldn't be touched.
  6.  *
  7.  * Flags:
  8.  * -c    don't try to create file if it doesn't already exist
  9.  *
  10.  * This program is in the public domain.
  11.  * David MacKenzie
  12.  * 6522 Elgin Lane
  13.  * Bethesda, MD 20817
  14.  *
  15.  * Latest revision: 04/23/88
  16.  */
  17.  
  18. #include <time.h>
  19. #include <fcntl.h>
  20.  
  21. long    timep[2];        /* two elements containing current time */
  22.  
  23. _main(argc, argv)
  24.     int     argc;
  25.     char  **argv;
  26. {
  27.     void    usage();
  28.     int     trycreat = 1;    /* try to create nonexistant file? */
  29.     int     optind;
  30.     int     errs = 0;        /* # of files we couldn't touch */
  31.  
  32.     (void) time(&timep[0]);
  33.     timep[1] = timep[0];
  34.  
  35.     for (optind = 1; optind < argc && *argv[optind] == '-'; ++optind)
  36.     while (*++argv[optind])
  37.         if (*argv[optind] == 'c')
  38.         trycreat = 0;
  39.         else
  40.         usage();
  41.  
  42.     if (optind == argc)
  43.     usage();
  44.  
  45.     for (; optind < argc; ++optind)
  46.     errs += touch(argv[optind], trycreat);
  47.  
  48.     exit(errs);
  49. }
  50.  
  51. /*
  52.  * Update the time of file.
  53.  * Return 0 if ok, 1 if error.
  54.  */
  55.  
  56. touch(file, trycreat)
  57.     char   *file;
  58.     int     trycreat;
  59. {
  60.     int     fd;
  61.  
  62.     if (utime(file, timep) == -1 &&
  63.     (!trycreat || (fd = open(file, O_CREAT)) == -1)) {
  64.     perror(file);
  65.     return 1;
  66.     }
  67.     (void) close(fd);
  68.     return 0;
  69. }
  70.  
  71. void
  72. usage()
  73. {
  74.     printf("Usage: touch [-c] file...\n");
  75.     exit(1);
  76. }
  77.